<?php
/**
 * Description of IndexController
 *
 * @author minhhq
 */
class Ajax_IndexController extends Zend_Controller_Action {
    public function init() {
        $this->_helper->layout()->disableLayout();
        $translate = Zend_Registry::get("translate");
        $this->view->translate = $translate;
        $this->view->facebook = MKT_Facebook::getInstance();
    }
    
    public function getactionAction() {
        $id_social_page = $this->_getParam("pid");
        $id_social_user = $this->_getParam("uid");
        $action_type    = $this->_getParam("at");
        $offset         = $this->_getParam("offset");
        $time           = $this->_getParam("time");
        
        $page_db = new Core_Model_DbTable_Pages();
        $page_info = $page_db->getPageInfo($id_social_page);
        
        if ($page_info['reset_period'] == 'w') {
            $start_time = MKT_Util::getFirstDayOfWeek($time);
            $end_time = MKT_Util::getLastDayOfWeek($time);
        } else {
            $start_time = MKT_Util::getFirstDayOfMonth($time);
            $end_time = MKT_Util::getLastDayOfMonth($time);
        }
        
        if (!empty($id_social_page) && !empty($id_social_user)) {
            $user_action_db = new Core_Model_DbTable_UserActions();
            $actions = $user_action_db->getActions($id_social_page, $id_social_user, $start_time, $end_time, $action_type, $offset);
            $this->view->actions = $actions;
        }
    }
    
    public function gettopfansAction() {
        $id_social_page = $this->_getParam("pid");
        $type           = $this->_getParam("type");
        $offset         = $this->_getParam("offset");
        $time           = $this->_getParam("time", time());
        $id_social_user = $this->_getParam("uid");
        
        //process time section
        $page_db = new Core_Model_DbTable_Pages();
        $page_info = $page_db->getPageInfo($id_social_page);
        if ($page_info['reset_period'] == 'w') {
            $start_time = MKT_Util::getFirstDayOfWeek($time);
            $end_time = MKT_Util::getLastDayOfWeek($time);
        } else {
            $start_time = MKT_Util::getFirstDayOfMonth($time);
            $end_time = MKT_Util::getLastDayOfMonth($time);
        }     
        
        $total_action = new Core_Model_DbTable_TotalActions();
        $fans = $total_action->getTopFans($id_social_page, $start_time, $end_time, $type, $offset);
        
        $this->_checkVoted($fans, $id_social_page, $id_social_user,$start_time, $end_time);
        $this->view->fans   = $fans;
        $this->view->offset = $offset + FAN_PER_PAGE;
    }    
    
    public function instructionAction() {
        $this->_helper->viewRenderer->setNoRender();
        $id_social_page = $this->_getParam("pid");
        $page_db = new Core_Model_DbTable_Pages();
        $instruction = $page_db->getInstruction($id_social_page);
        echo $instruction;
    }
    
    public function voteAction() {
        $this->setAjaxCall();
        
        $id_social_page = $this->_getParam("pid");
        $voted_id = $this->_getParam("voted_id");
        $id_social_user = $this->_getParam("uid");
        $start_time = $this->_getParam("start_time");
        $end_time = $this->_getParam("end_time");
        
        $user_action_db = new Core_Model_DbTable_UserActions();
        $voted = $user_action_db->checkVoted($id_social_page, $id_social_user, array($voted_id), $start_time, $end_time);
        
        if (!$voted[$voted_id]) {
            $data = array (
                'id_social_page'        => $id_social_page,
                'id_social_user'        => $voted_id,
                'target_id_social_from' => $id_social_user,
                'type'                  => VOTE,
                'created_time'          => time()
            );

            try {
                $user_action_db->insert($data);

                $total_db = new Core_Model_DbTable_TotalActions();
                $data['from_name'] = "";
                $total_db->insertActions(array($data), VOTE);
            } catch (Exception $e) {
                if (MKT_Util::isDebugMode()) {
                    Zend_Debug::dump($e);
                }
                $log = Zend_Registry::get("log");
                $log->log("{$e->getMessage()} when insert vote");

                echo 0;
            }
        }
        
        echo 1;
    }
    
    public function unvoteAction() {
        $this->setAjaxCall();
        
        try {
            $vote_id = $this->_getParam("vote_id");
            $user_action_db = new Core_Model_DbTable_UserActions();
            $vote_detail = $user_action_db->fetchRow("id = $vote_id");
            if (!empty($vote_detail)) {
                $id_social_user = $vote_detail->id_social_user;
                $id_social_page = $vote_detail->id_social_page;
                $time = Core_Util::getBeginDate($vote_detail->created_time);

                $page_db = new Core_Model_DbTable_Pages();
                $vote_point = $page_db->getPoint($id_social_page, VOTE);

                $total_db = new Core_Model_DbTable_TotalActions();
                $total_db->update(
                    array (
                        'votes' => new Zend_Db_Expr("votes - 1"),
                        "total_point" => new Zend_Db_Expr("total_point - $vote_point")
                    ), array (
                        "id_social_page = '$id_social_page'", 
                        "id_social_user = '$id_social_user'", 
                        "time = '$time'"
                    )
                );

                $user_action_db->delete("id = $vote_id");
            }
        } catch (Exception $e) {
            if (MKT_Util::isDebugMode()) {
                Zend_Debug::dump($e);
            }
            $log = Zend_Registry::get("log");
            $log->log("{$e->getMessage()} when unvote", 0);
            echo 0;
            return;
        }
        echo 1;
    }
    
    public function deletebannerAction() {
        $this->setAjaxCall();
        
        $id_social_page = $this->_getParam("pid");
        $page_db = new Core_Model_DbTable_Pages();
        try {
            $banner_image = $page_db->getPageInfo($id_social_page)->banner_image;

            $upload_path = MKT_Util::getConfig("upload_path");
            unlink($upload_path . '/' . $banner_image);

            $page_db->update(array("banner_image" => NULL), "id_social_page = '$id_social_page'");
            
            echo 1;
        } catch (Exception $e) {
            if (MKT_Util::isDebugMode()) {
                Zend_Debug::dump($e);
            }
            $log = Zend_Registry::get("log");
            $log->log("{$e->getMessage()} when delete banner image", 0);
            
            echo 0;
        }
    }
    
    private function _checkVoted(&$fans, $id_social_page, $id_social_user, $start_time, $end_time) {
        $voted_ids = array();
        foreach ($fans as $fan) {
            $voted_ids[] = $fan['id_social_user'];
        }

        $user_action_db = new Core_Model_DbTable_UserActions();
        $voted_result = $user_action_db->checkVoted($id_social_page, $id_social_user, $voted_ids, $start_time, $end_time);

        foreach ($fans as &$fan) {
            $fan['voted'] = $voted_result[$fan['id_social_user']];
        }
    }
    
        
    protected function setAjaxCall() {
        $this->_helper->viewRenderer->setNoRender();
    }    
}

?>
